<template>
  <div>
    <zbTable ref="tableRef" :api-fun="get_list" @action="action">
      <template #toolbar>
        <a-space>
          <a-button type="primary" class="ele-btn-icon" @click="openEdit()">
						<template #icon>
							<plus-outlined />
						</template>
						<span>新建</span>
					</a-button>
          <a-button type="dashed" class="ele-btn-icon" @click="daochu()">
            <template #icon>
              <upload-outlined />
            </template>
            <span>导出</span>
          </a-button>
        </a-space>
      </template>
    </zbTable>
    <!-- 新增/编辑弹窗 -->
		<user-edit v-if="showEdit" v-model:visible="showEdit" :actionStatus="actionStatus" :editData="editData" @done="done" />
    <!-- 同意弹窗 -->
    <check-back
      v-if="showBack"
      v-model:visible="showBack"
      :code="code"
      @done="done"
    />
    <!-- 拒绝弹窗 -->
    <check-data v-if="showCheck" v-model:visible="showCheck" :editData="editData" @done="done" />
  </div>
</template>
<script setup>
  import { ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { actionFn } from '@/utils/action.js';
	import { daochuFn } from '@/utils/daochu.js';
  import { PlusOutlined, UploadOutlined } from '@ant-design/icons-vue';
  import zbTable from '@/components/zbTable/jewelryIndex.vue';
  import checkData from './components/check-data.vue';
  import checkBack from './components/check-back.vue';
  import userEdit from './components/user-edit.vue';
  import { get_list, confirm_receive ,cancel_back,del_receipts,verify_back,confirm_back,cancel_verify} from '@/api/jewelry/receipts_back';
// 是否显示编辑弹窗
  const showEdit = ref(false);
  const tableRef = ref(null);
  const showCheck = ref(false);
  const showBack = ref(false);
  const code = ref(null);
  const actionStatus = ref('add');
  // 编辑数据
  const editData = ref({});
  const actionData = ref({});
/* 打开添加弹窗 */
const openEdit = row => {
	editData.value.id = '';
	editData.value.code = '';
	showEdit.value = true;
	actionStatus.value = 'add';
	actionData.value = { url: '/jewelry/receipts_back/add' };
};
  const action = (data) => {
    console.log("ata.action_data.type",data.action_data.type)
    editData.value.id = data.row_data.id;
    editData.value.code = data.row_data.code;
    actionData.value = data.action_data;
    if(data.action_data.type == 'edit') {
      showEdit.value = true;
      actionStatus.value = 'edit';
	  } else if (data.action_data.type == 'agree') {
      showBack.value = true;
    } else if (data.action_data.type == 'reject') {
      showCheck.value = true;
    } else if (data.action_data.type == 'cancel') {//作废
      Modal.confirm({
			title: '提示',
			content: '确定作废?',
			maskClosable: true,
			onOk: () => {
				cancelOpt();
			}
		});
    }else if (data.action_data.type == 'confirm') {//提交审核
          Modal.confirm({
          title: '提示',
          content: '确定提交审核?',
          maskClosable: true,
          onOk: () => {
            confirmPurchase();
          }
        });
    }else if (data.action_data.type == 'delete') {//删除
        Modal.confirm({
        title: '提示',
        content: '确定删除，删除后不可恢复?',
        maskClosable: true,
        onOk: () => {
          deleted();
        }
      });
    }else if (data.action_data.type == 'cancel_verify') {//取消审核
        Modal.confirm({
        title: '提示',
        content: '确定取消审核?',
        maskClosable: true,
        onOk: () => {
          cancelVerify();
        }
      });
    }else if (data.action_data.type == 'verify') {//审核
        showCheck.value = true;
    }

  };
    //取消审核
  const cancelVerify = () => {
    cancel_verify({ code: editData.value.code })
		.then(res => {
			reloadTable();
			message.success(res.msg);
		})
		.catch(e => {
			message.error(e);
		});
};
  //提交审核
  const confirmPurchase = () => {
    confirm_back({ code: editData.value.code })
		.then(res => {
			reloadTable();
			message.success(res.msg);
		})
		.catch(e => {
			message.error(e);
		});
};
//作废
  const cancelOpt = () => {
    cancel_back({ code: editData.value.code })
		.then(res => {
			reloadTable();
			message.success(res.msg);
		})
		.catch(e => {
			message.error(e);
		});
};
//删除
const deleted = () => {
	del_receipts({ code: editData.value.code })
		.then(res => {
			reloadTable();
			message.success(res.msg);
		})
		.catch(e => {
			message.error(e);
		});
};
  const receive = (data) => {
    confirm_receive({
      code: data.row_data.code,
      receive_status: 1,
      receive_cause: ''
    })
      .then((res) => {
        reloadTable();
        message.success(res.msg);
      })
      .catch((e) => {
        message.error(e);
      });
  };
  const done = (form) => {
    actionFn(actionData.value, form, reloadTable);
    showEdit.value = false;
  };
  const reloadTable = () => {
    showCheck.value = false;
    showBack.value = false;
    tableRef.value.reload();
  };
	const daochu = ()=>{
	  let data = {
		  url:'jewelry/receipts_back/export',
		  params:{},
		  filename:'门店返仓列表'
	  }
	  daochuFn(data);
	};
</script>
